package com.yjlove.myplay.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.yjlove.myplay.R;
import com.yjlove.myplay.bean.ShowImageInfo;
import com.yjlove.myplay.utils.ImageFormatUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者 YJlvoe
 * 创建时间 2017/2/23.
 */
public class ImagesEditAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<ShowImageInfo> mImagesInfoList = new ArrayList<>();
    private List<Integer> deletePositions = new ArrayList<>();

    public ImagesEditAdapter(Context context, List<ShowImageInfo> imagesInfoList) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.mImagesInfoList = imagesInfoList;
    }

    @Override
    public int getCount() {
        return mImagesInfoList.size();
    }

    @Override
    public ShowImageInfo getItem(int position) {
        return mImagesInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.images_edit_gridview_item, parent,
                    false);
            viewHolder = new ViewHolder();
            viewHolder.mRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.image_edit_layout);
            viewHolder.mImageViewCheck = (ImageView) convertView.findViewById(R.id.image_edit_check);
            viewHolder.mImageView = (ImageView) convertView.findViewById(R.id.image_edit);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ImageLoader.getInstance().displayImage("file://" + mImagesInfoList.get(position).getImagePath() + mImagesInfoList.get(position).getImageName(), viewHolder.mImageView, ImageFormatUtil.getOptions());
        viewHolder.mImageViewCheck.setTag("unCheck");
        viewHolder.mRelativeLayout.setTag(viewHolder);
        deletePositions.clear();
        viewHolder.mRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewHolder holder = (ViewHolder) v.getTag();
                if (holder.mImageViewCheck.getTag().equals("unCheck")){
                    holder.mImageViewCheck.setImageResource(R.drawable.item_select_ok);
                    holder.mImageViewCheck.setTag(position);
                    deletePositions.add(position);
                    itemClickListener.OnItemClickListener(deletePositions);
                } else {
                    holder.mImageViewCheck.setImageResource(R.drawable.item_select_null);
                    holder.mImageViewCheck.setTag("unCheck");
                    for (int i=0; i<deletePositions.size(); i++){
                        if (deletePositions.get(i) == position){
                            deletePositions.remove(i);
                            itemClickListener.OnItemClickListener(deletePositions);
                        }
                    }
                }
            }
        });
        return convertView;
    }

    private final class ViewHolder {
        RelativeLayout mRelativeLayout;
        ImageView mImageViewCheck;
        ImageView mImageView;
    }
    private ItemClickListener itemClickListener;

    public void getOnItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void OnItemClickListener(List<Integer> deletePositions);
    }
}
